#pragma once
#include <iostream>
#include <string>
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include <unordered_map>
#include <vector>
#include <cstdlib>
#include <assert.h>
//MYSQL
#include"include/mysql.h"
namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    struct Question
    {
        /* data */
        std::string number;
        std::string title;
        std::string star;
        std::string desc;
        std::string header;
        std::string tail;
        int cpu_limit;
        int mem_limit;
    };

    const string oj_questions="oj_questions";
    const string host="127.0.0.1";
    const string user="oj_client";
    const string passwd="123456";
    const int port=3306;
    const string db="oj";
    class Model
    {
    private:
    public:
        Model()
        {
        }
        bool QueryMysql(const std::string &sql,vector<Question> *out)
        {
            //创建MySQL句柄
            MYSQL *my=mysql_init(nullptr);
            //连接数据库
            if(nullptr==mysql_real_connect(my,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,nullptr,0))
            {
                LOG(FATAL)<<"连接数据库失败！"<<"\n";
                return false;
            }
            mysql_set_character_set(my,"utf-8");
            LOG(INFO)<<"连接数据库成功！"<<"\n";
            if(0!=mysql_query(my,sql.c_str()))
            {
                LOG(WARNING)<<sql.c_str()<<"execut error!"<<"\n";
                return false;
            }

            MYSQL_RES *res=mysql_store_result(my);
            //行和列的值
            int rows=mysql_num_rows(res);
            int cols=mysql_num_fields(res);

            for(int i=0;i<rows;i++)
            {
                struct Question q;
                MYSQL_ROW row=mysql_fetch_row(res);
                q.number=row[0];
                q.title=row[1];
                q.star=row[2];
                q.desc=row[3];
                q.header=row[4];
                q.tail=row[5];
                q.cpu_limit=stoi(row[6]);
                q.mem_limit=stoi(row[7]); 
                out->push_back(q);
            }
            sort(out->begin(),out->end(),[](Question a,Question b){
                return a.number<b.number;
            });

            //释放连接
            free(res);
            //关闭连接
            mysql_close(my);

        }
        bool GetAllQuestions(vector<Question> *out)
        {
            string sql="select * from ";
            sql+=oj_questions;
            return QueryMysql(sql,out);
        }
        bool GetOneQuestion(const std::string &number,Question *q)
        {
            bool res=false;
            string sql="select * from ";
            sql+=oj_questions;
            sql+=" where number=";
            sql+=number;
            vector<Question> result;
            if(QueryMysql(sql,&result))
            {
                if(result.size()==1)
                {
                    *q=result[0];
                    res=true;
                }
            }
            return res;
        }
        ~Model()
        {

        }
    };

}